package com.yutian.selfservice.service.impl;

import com.yutian.selfservice.support.domain.UploadFile;
import com.yutian.selfservice.support.mapper.UploadFileMapper;
import com.yutian.selfservice.support.upload.handler.Upload;
import com.yutian.selfservice.service.UploadService;
import com.yutian.selfservice.support.upload.handler.UploadResult;
import com.yutian.selfservice.support.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;


@Service
@Slf4j
public class UploadServiceImpl implements UploadService {

    @Autowired
    private Upload upload;
    @Autowired
    private UploadFileMapper uploadFileMapper;

    @Override
    public Result upload(HttpServletRequest request) {
        Result result = upload.upload(request);
        if (result.isSuccess()) {
            UploadResult uploadResult = (UploadResult) result.getData();
            UploadFile file = new UploadFile()
                    .setId(uploadResult.getId())
                    .setFileName(uploadResult.getFileName())
                    .setRemoteUrl(uploadResult.getRemoteUrl())
                    .setUserId(0l);
            uploadFileMapper.insert(file);
        }
        return result;
    }
}
